import QtQuick 1.1
import com.nokia.meego 1.0
import TextEditor 1.0

PageTemplate {
    id: editorPage

    CaptionItem {
        id: captionItem
        Row {
            id: editButtons
            parent: captionItem.captionContentRow
            width: parent.width
            property real buttonWidth: width / 6
            Button {
                id: undoButton
                width: editButtons.buttonWidth
                //text: appWindow.inPortrait ? '' : qsTr("Undo")
                iconSource: textEditorItem.imageFilePath('edit-undo' + (enabled ? '' : '-disabled') + '.png')
                enabled: false
                onClicked: {
                    textEditorItem.undo();
                }
            }
            Button {
                id: redoButton
                width: editButtons.buttonWidth
                //text: appWindow.inPortrait ? '' : qsTr("Redo")
                iconSource: editor.textEditorItem.imageFilePath('edit-redo' + (enabled ? '' : '-disabled') + '.png')
                enabled: false
                onClicked: {
                    textEditorItem.redo();
                }
            }
            Button {
                id: cutButton
                width: editButtons.buttonWidth
                //text: appWindow.inPortrait ? '' : qsTr("Cut")
                iconSource: editor.textEditorItem.imageFilePath('edit-cut' + (enabled ? '' : '-disabled') + '.png')
                enabled: false
                onClicked: {
                    textEditorItem.cut();
                }
            }
            Button {
                id: copyButton
                width: editButtons.buttonWidth
                //text: appWindow.inPortrait ? '' : qsTr("Copy")
                iconSource: editor.textEditorItem.imageFilePath('edit-copy' + (enabled ? '' : '-disabled') + '.png')
                enabled: false
                onClicked: {
                    textEditorItem.copy();
                }
            }
            Button {
                id: pasteButton
                width: editButtons.buttonWidth
                //text: appWindow.inPortrait ? '' : qsTr("Paste")
                iconSource: editor.textEditorItem.imageFilePath('edit-paste' + (enabled ? '' : '-disabled') + '.png')
                onClicked: {
                    textEditorItem.paste();
                }
            }
            Button {
                id: searchButton
                width: editButtons.buttonWidth
                //text: appWindow.inPortrait ? '' : qsTr("Find")
                iconSource: editor.textEditorItem.imageFilePath('edit-find.png')
                checkable: true
                onClicked: {
                    setSearchEnabled(checked);
                }
            }
        }
    }

    Rectangle {
        id: captionRect
        y: captionItem.height
        width: editorPage.width
        height: captionColumn.height
        color: theme.inverted ? '#212121' : '#f0f0f0'
        property int buttonWidth: captionColumn.width / 6
        Column {
            id: captionColumn
            x: 5
            width: parent.width - 10

            Row {
                id: searchRow
                anchors {
                    left: captionColumn.left
                    right: captionColumn.right
                }
                visible: false
                TextField {
                    id: searchEdit
                    width: captionColumn.width - previousSearchButton.width - nextSearchButton.width;
                    text: ''
                    onVisibleChanged: {
                        if (visible)
                            selectAll();
                    }
                }
                Button {
                    id: previousSearchButton
                    iconSource: "image://theme/icon-m-toolbar-previous" + (theme.inverted ? "-white" : "")
                    width: height * 1.3
                    //enabled: false
                    onClicked: {
                        if (!editorItem.searchPrevious(searchEdit.text)) {
                            //enabled = false;
                        }
                    }
                }
                Button {
                    id: nextSearchButton
                    iconSource: "image://theme/icon-m-toolbar-next" + (theme.inverted ? "-white" : "")
                    width: height * 1.3
                    //enabled: false
                    onClicked: {
                        if (!editorItem.searchNext(searchEdit.text)) {
                            //enabled = false;
                        }
                    }
                }
            }
        }
    }

    /*
    property variant inPortrait: appWindow.inPortrait
    onInPortraitChanged: {
    }
    */

    property variant textEditorItem: editorItem

    TextEditorItem {
        id: editorItem
        anchors {
            top: captionRect.bottom
            bottom: editorPage.bottom
            left: editorPage.left
            right: editorPage.right
            margins: 0
        }
        Component.onCompleted: {
            editorItem.undoAvailable.connect( function(available) {
                undoButton.enabled = available;
            } );
            editorItem.redoAvailable.connect( function(available) {
                redoButton.enabled = available;
            } );
            editorItem.copyAvailable.connect( function(available) {
                cutButton.enabled = available;
                copyButton.enabled = available;
            } );
        }
        Component.onDestruction: {
            console.log('TextEditorItem.onDestruction');
        }
    }

    function setSearchEnabled(searchEnabled) {
        if (searchEnabled) {
            searchRow.visible = true;
            searchEdit.focus = true;
        } else {
            searchRow.visible = false;
        }
    }

    property variant currentFilePath: null;

    onCurrentFilePathChanged: {
        if (currentFilePath === null || currentFilePath === undefined) {
            appWindow.setCaptionText('[' + qsTr('Unsaved') + ']');
        } else {
            appWindow.setCaptionText(currentFilePath);
        }
    }

    function newDocument(file) {
        editorItem.clear();
        currentFilePath = file;
        return true;
    }

    function loadDocument(file) {
        var ok = editorItem.load(file);
        if (ok) {
            currentFilePath = file;
        }
        return ok;
    }

    function saveDocument(file) {
        var ok = editorItem.save(file);
        if (ok) {
            currentFilePath = file;
        }
        return ok;
    }

    onVisibleChanged: {
        console.log('EditPage.onVisibleChanged visible=' + visible);
    }

    Component.onCompleted: {
    }

    Component.onDestruction: {
        console.log('EditPage.onDestruction');
    }
}
